import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '@/components/Welcome.vue'
import User from '@/components/User.vue'
import Rights from '@/components/power/Rights'
import Roles from '@/components/power/Roles'
import GoodsList from '@/components/goods/List.vue'
import Add from '@/components/goods/Add'
import Params from '@/components/goods/Params'
import Cate from '@/components/goods/Cate'
import Order from '@/components/order/Order'


Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
    },
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [{
                path: '/welcome',
                component: Welcome
            }, {
                path: '/users',
                component: User
            }, {
                path: '/rights',
                component: Rights
            }, {
                path: '/roles',
                component: Roles
            }, {
                path: '/goods',
                component: GoodsList
            },
            {
                path: '/goods/add',
                component: Add
            }, {
                path: '/params',
                component: Params
            },
            {
                path: '/categories',
                component: Cate
            }, { path: '/orders', component: Order },
        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})


router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    const tokenStr = window.localStorage.getItem('token');
    if (!tokenStr) return next('/login')
    return next()
})

export default router